﻿using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Nextensions.GUI
{
    internal class PathCompleter
    {
        private Control _target;
        private SelectableListWindow _listWindow = new SelectableListWindow();
        private Form _owner;
        private IWin32Window _iWin32Owner;
        private bool _ownerLoaded = false;
        private string _dir = null;

        internal PathCompleter(ref PcComboBox Target)
        {
            _target = Target;
            _target.TextChanged += _target_TextChanged;
            _target.KeyDown += new KeyEventHandler(_target_KeyDown);
            _target.KeyUp += new KeyEventHandler(_target_KeyUp);
            Control prt = _target.Parent;

            while (!(prt is IWin32Window))
            {
                prt = prt.Parent;
            }

            _iWin32Owner = prt;
            _owner = (Form)prt;
            _owner.Load += new EventHandler(frm_Load);
            _owner.LocationChanged += new EventHandler(frm_LocationChanged);
            _listWindow.Load += new EventHandler(_list_Load);
            _listWindow.Deactivate += new EventHandler(_listWindow_Deactivate);
        }

        void _target_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void _target_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
                e.Handled = true;
        }

        private void _listWindow_Deactivate(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(_listWindow.Path))
                _target.Text = _listWindow.Path;
        }

        private Point GetLocation()
        {
            Point loc;// = _owner.Location;
            loc = _target.PointToScreen(new Point(0, _target.Height));

            return loc;
        }

        private void frm_LocationChanged(object sender, EventArgs e)
        {
            Point loc = GetLocation();
            _listWindow.Location = loc;
            System.Diagnostics.Debug.WriteLine("Window location changed = " + loc);
        }

        private void frm_Load(object sender, EventArgs e)
        {
            _ownerLoaded = true;
        }

        private void _target_TextChanged(object sender, System.EventArgs e)
        {
            if (!_ownerLoaded)
                return;

            if(String.IsNullOrEmpty(_target.Text))
                return;

            Match mDir = Regex.Match(_target.Text, @"(?<dir>^(\\[\w\-\d]|[A-Z]\:\\)([^\\]+\\)*)(?<rel>[^\\]+)?$");
            
            if (!mDir.Success)
                return;

            string dir = mDir.Groups["dir"].Value;
            string rel = mDir.Groups["rel"].Value;

            if (dir != _dir)
            {
                if (Directory.Exists(_target.Text))
                {
                    string[] dirs = Directory.GetDirectories(_target.Text, "*", SearchOption.TopDirectoryOnly);
                    _listWindow.SetItems(dirs);
                }
            }

            // Filter out items to match the part of the folder or file that the user has entered
            if(!String.IsNullOrEmpty(dir))
            {
                _listWindow.SetStartsWithFilter(dir);
            }

            _dir = dir;

            if (!_listWindow.Visible)
            {
                Point loc = GetLocation();

                //_list.Location = loc;
                _listWindow.Show(_iWin32Owner);
                //((ComboBox)_target).sele
                //_listWindow.Activate();
            }
        }

        private void _list_Load(object sender, EventArgs e)
        {
            _listWindow.Location = GetLocation();
        }
    }
}
